<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/23
 * Time: 12:00
 */
class SampleskuModel extends BaseModel
{
    private $_table;
    protected $_name = 'free_sample_sku';
    protected $_isbug = false;


    public function getSampleOrder($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();

        $sql = $db->select ()->from ( $this->_name ,"$this->_name.id")
            ->joinLeft ("free_sample", "$this->_name.sample_id = free_sample.id", "")
            ->where ( $where )->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        return $db->fetchOne ( $sql );
    }


    /**
     * 免费试样列表
     * @param int $start
     * @param int $count
     * @param string $where
     * @param string $order
     * @return array
     */
    public function getSampleOrderList($start=0,$count=0,$where ="1=1",$order ="create_time DESC"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )->joinLeft("free_sample","$this->_name.sample_id = free_sample.id",array("receive_business_name","receive_name","receive_mobile","status","sample_key"))
            ->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name","product_code"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->where ( $where )->order($order)->__toString ();
        $sql .= " limit " . (($start - 1)) * $count . "," . $count;
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchAll( $sql );
    }


    /**
     * 免费试样总计
     * @param string $where
     * @return string
     */
    public function getSampleOrderTotal($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name ,"count(*)")->joinLeft("free_sample","$this->_name.sample_id = free_sample.id","")
            ->joinLeft ( "product", "$this->_name.product_id = product.id", "")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "")
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "" )
            ->where ( $where )->__toString ();
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchOne( $sql );
    }


    public function getSampleOrderDetail($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )->joinLeft("free_sample","$this->_name.sample_id = free_sample.id",array("receive_business_name","receive_name","receive_mobile","status","bill_sn","logistics_company","sample_key","remark","province_id","city_id","area_id","detail_addr"))
            ->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name","product_code"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->where ( $where )->__toString ();
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchRow( $sql );
    }


}